from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from page_object.driver.AndroidClient import AndroidClient
import yaml


class BasePage(object):
    '''
    driver可以替换成不同的平台，比如小程序，ios， webview
    其他的页面都继承 BasePage，AndroidClient.driver写成 self.driver 也方便些
    '''
    def __init__(self):
        self.driver = AndroidClient.driver

    @classmethod
    def getDriver(cls):
        cls.driver = AndroidClient.driver
        return cls.driver
    @classmethod
    def getClient(cls):
        return AndroidClient

    def find(self, kv) ->WebElement:
        '''
        封装find方法，之后只需要调用find方法就好，
        :param kv:
        :return:
        '''
        # todo:处理各类弹框
        return self.driver.find_element(*kv)
    def find_by_xpath(self, text) ->WebElement:
        """
        封装xpath方法，就不用写xpath了
        :param text: 定位参数
        :return:
        """
        return self.find((By.XPATH, "//*[@text='%s']" %text))
    def loadSteps(self, po_path, key, **kwargs):
        # open()方法读取方式需要改为'rb',原来是'r'只读
        file = open(po_path, 'rb')
        po_data = yaml.load(file, Loader=yaml.FullLoader)
        po_method = po_data[key]
        for step in po_method:
            element = self.driver.find_element(by=step['by'], value=step['locator'])
            actions = str(step['action']).lower()
            if actions == 'click':
                element.click()
            elif actions == 'sendkeys':
                text = str(step['text'])
                #不懂
                for k, v in kwargs.items():
                    text = text.replace("$%s" %k, v)
                element.send_keys(text)
            else:
                print('game over')




























